/**
 * 
 */
package com.nd.android.payment.ui.param;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.view.KeyEvent;
import android.widget.Toast;
import antistatic.spinnerwheel.AbstractWheel;
import antistatic.spinnerwheel.OnWheelScrollListener;

import com.common.StarAppConst;
import com.common.allCommon.CommUtil;
import com.common.widget.CommonTipDlg;
import com.common.widget.StarappWaitDlg;
import com.common.widget.CommonTipDlg.OnCommonDlgClickListener;
import com.nd.android.payment.R;
import com.nd.android.payment.sdk.dialog.SDKDialogUtils;
import com.nd.android.payment.ui.dialog.CommonRechargeDlg.OnCommonRechargeClickListener;
import com.nd.android.payment.ui.task.ICommonBuyCallBack;
import com.nd.android.payment.util.PaymentUtils;

/**
 * 
 * <br>
 * Created 2014-11-3 下午5:44:28
 * 
 * @version
 * @author zhangqr
 * 
 * @see
 */

public class CommonBuyDiamond implements ICommonRecharge  {

    /** 标题 */
    private String mStrTitle;
    
    /**
     * 标题 - 剩余的提示
     */
    private String mStrTitleRest;
    
    /**
     * 标题 - 兑换率的提示
     */
    private String mStrTitleRate;

    /** 滚动框里面默认显示第几个 */
    private int mIntDefaultItemIndex;

    /** 剩余币种数量的提示 */
    private String mStrRestTip;

    /** 兑换率提示 */
    private String mStrRateTip;

    /** 提示 */
    private String mStrTip;

    /** 左按钮文本 */
    private String mStrBtnLeft;

    /** 右按钮文本 */
    private String mStrBtnRight;

    /** 所加载的layout资源ID */
    private int mLayoutResID;
    
    /** 是否显示滚动框 */
    private boolean mIsShowWheelView = true;

    /** 滚动框显示数据 */
    private ArrayList<Object> mDataList = new ArrayList<Object>();
    
    /** 按钮点击监听 */
    private OnCommonRechargeClickListener mListener;
    
    /** 滚动框滚动监听 */
    private OnWheelScrollListener mWheelScrollListener;
    
    /** 上下文 */
    private Activity mActivity;
    
    /**
     * 当操作结束时是否关闭窗口
     */
    private boolean isDismissWhenDone = true;
    
    /**
     * 回调
     */
    private ICommonBuyCallBack mCallBack;
    
    private int mActionType;
    
    /** 所需金币 */
    private int mIntNeedDiamond;
    
    /**
     * loading框
     */
    private Dialog mWaitDlg;
    
    /**
     * 充值星钻
     * Creates a new instance of CommonBuyDiamond.
     * <br>Created 2014-11-11 上午11:24:54
     * @param activity
     */
    public CommonBuyDiamond(Activity activity, ICommonBuyCallBack diamondCallBack) {
        this(activity, StarAppConst.PAY.TYPE_RECHARGE, -1, null, null, diamondCallBack);
    }
    
    /**
     * 消费时 - 购买星钻
     * Creates a new instance of CommonBuyDiamond.
     * <br>Created 2014-11-14 上午11:38:09
     * @param activity          activity
     * @param needDiamond       所需的星钻数
     * @param diamondCallBack   回调
     */
    public CommonBuyDiamond(Activity activity, int needDiamond,
            ICommonBuyCallBack diamondCallBack) {
        this(activity, StarAppConst.PAY.TYPE_BUY, needDiamond, null, null, diamondCallBack);
    }
    
    /**
     * 实例化 - 充值星钻
     * Creates a new instance of CommonBuyDiamond.
     * <br>Created 2014-11-14 上午11:38:35
     * @param activity          activity
     * @param actionType        
     *      动作类型。消费/充值.
     *      StarAppConst.PAY.TYPE_RECHARGE / StarAppConst.PAY.TYPE_BUY
     * @param needDiamond       所需的星钻数
     * @param clickListener     点击事件监听
     * @param wheelListener     滚动框滚动监听
     * @param diamondCallBack   回调
     */
    public CommonBuyDiamond(Activity activity,
            int actionType, int needDiamond,
            OnCommonRechargeClickListener clickListener,
            OnWheelScrollListener wheelListener,
            ICommonBuyCallBack diamondCallBack) {
        this.mActivity = activity;
        
        this.mActionType = actionType;
        
        this.mIntNeedDiamond = needDiamond;
        
        this.mListener = clickListener;
        this.mWheelScrollListener = wheelListener;
        this.mCallBack = diamondCallBack;
        
        initValue();
        
        initClickListener();
        
        initWheelScrollListener();
        
        mWaitDlg = new StarappWaitDlg(mActivity, mActivity.getString(R.string.pay_loading),
                R.style.Style_Self_Info_Dlg);

        mWaitDlg.setCanceledOnTouchOutside(false);
        mWaitDlg.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                // 屏蔽Search键(因为按搜索按键会被关闭)
                switch (event.getKeyCode()) {
                case KeyEvent.KEYCODE_SEARCH:
                    return true;
                case KeyEvent.KEYCODE_BACK:
                    return true;
                default:
                    return false;
                }
            }
        });
    }
    
    private void initValue() {
        isDismissWhenDone = true;
        
        mIsShowWheelView = true;
        
        /** 所加载的layout资源ID */
//        mLayoutResID = R.layout.common_pay_buy_diamond;
        mLayoutResID = 0;
        
        /** 标题 */
        mStrTitle = mActivity.getResources().getString(R.string.pay_buy_diamond_title);
        mStrTitleRest = "";
        mStrTitleRate = "";
        
        /** 滚动框里面默认显示第几个 */
        mIntDefaultItemIndex = 0;
        
        /** 滚动框显示数据 */
        int start = StarAppConst.PAY.BUY_DIAMOND_MIN;
        if(StarAppConst.PAY.TYPE_RECHARGE == mActionType) {//充值
            for(int i=0; start < StarAppConst.PAY.BUY_DIAMOND_MAX; i++){
                start = StarAppConst.PAY.BUY_DIAMOND_MIN + i*StarAppConst.PAY.BUY_DIAMOND_STEP;
                mDataList.add(start);
            }
            
        } else if(StarAppConst.PAY.TYPE_BUY == mActionType) {//消费
            
            if(mIntNeedDiamond % StarAppConst.PAY.BUY_DIAMOND_STEP == 0) {
                start = mIntNeedDiamond;
            } else {
                int ret = mIntNeedDiamond / StarAppConst.PAY.BUY_DIAMOND_STEP;
                start = (ret + 1) * StarAppConst.PAY.BUY_DIAMOND_STEP;
            }
            
            while (start < StarAppConst.PAY.BUY_DIAMOND_MAX) {
                mDataList.add(start);
                start = start + StarAppConst.PAY.BUY_DIAMOND_STEP;
            }
        }
        
        mStrRestTip = "";
        mStrRateTip = "";
        mStrTip = "";
        
        mStrBtnLeft = mActivity.getResources().getString(R.string.pay_buy_diamond_lbtn);
        mStrBtnRight = mActivity.getResources().getString(R.string.pay_buy_diamond_rbtn);
        
    }

    /** 
     * 按钮点击监听
     * 
     * <br>Created 2014-11-4 下午2:08:27
     * @author       zhangqr
     */
    @SuppressLint("ShowToast")
    private void initClickListener() {
        if(mListener == null) {
            mListener = new OnCommonRechargeClickListener() {
                
                @Override
                public void onRightBtnClicked(final int count) {
                    isDismissWhenDone = true;
                    
                    if (!CommUtil.JudgeNetWorkStatus(mActivity)) {//网络不可用
                        String failConnect = mActivity.getString(R.string.pay_failed_connect);
                        if(mCallBack != null) {
                            mCallBack.onFail(failConnect);
                        } else {
                            Toast.makeText(mActivity, failConnect, Toast.LENGTH_SHORT);
                        }
                        return ;
                    }
                    if (mWaitDlg != null) {
                        mWaitDlg.show();
                    }
                    
                    SDKDialogUtils.openNdUniPayAsynDialog(mActivity, count, new ICommonBuyCallBack() {
                        
                        @Override
                        public void onSuccess(int diamondCount, int goldCount) {
                            if ((mWaitDlg != null) && (mWaitDlg.isShowing())) {
                                mWaitDlg.dismiss();
                            }
                            mCallBack.onSuccess(diamondCount, goldCount);
                        }
                        
                        @Override
                        public void onFinish() {
                            if ((mWaitDlg != null) && (mWaitDlg.isShowing())) {
                                mWaitDlg.dismiss();
                            }
                            mCallBack.onFinish();
                        }
                        
                        @Override
                        public void onFail(String msg) {
                            if ((mWaitDlg != null) && (mWaitDlg.isShowing())) {
                                mWaitDlg.dismiss();
                            }
                            mCallBack.onFail(msg);
                        }
                    });
                    
                }
                
                @Override
                public void onLeftBtnClicked() {
                    isDismissWhenDone = true;
                }
            };
        }
    }
    
    /** 
     * 滚动框滚动监听
     * 
     * <br>Created 2014-11-4 下午2:09:03
     * @author       zhangqr
     */
    private void initWheelScrollListener() {
        if(mWheelScrollListener == null) {
            mWheelScrollListener = new OnWheelScrollListener() {
                /**
                 * 开始
                 * <br>Created 2014-11-1 下午5:06:50
                 * @param wheel
                 * @author       张清荣
                 */
                public void onScrollingStarted(AbstractWheel wheel) {
                    
                };
                
                /**
                 * 结束
                 * <br>Created 2014-11-1 下午5:06:50
                 * @param wheel
                 * @author       张清荣
                 */
                @Override
                public void onScrollingFinished(AbstractWheel wheel) {
                    
                }
                
            };
        }
    }
    
    /**
     * 注销登录确认框
     * 
     * <br>Created 2014-11-21 上午10:24:26
     * @author       zhangqr
     */
    private void sureToLogout() {
        CommonTipDlg mLogOutCommonTipDlg = new CommonTipDlg(mActivity,
                mActivity.getString(R.string.pay_logout_tip),
                mActivity.getString(R.string.pay_logout_btn_left),
                mActivity.getString(R.string.pay_logout_btn_right),
                R.style.common_dialog_style);
        mLogOutCommonTipDlg.setLayoutResID(R.layout.common_tip_dlg_pink);
        mLogOutCommonTipDlg.setOnListener(logoutClickListener);
        mLogOutCommonTipDlg.show();
    }
    
    /** 注销登录确认框点击事件监听 */
    private OnCommonDlgClickListener logoutClickListener = new OnCommonDlgClickListener() {      
        @Override
        public void onRightBtnClicked() {
            doLogout();
        }
        
        @Override
        public void onLeftBtnClicked() {
            
        }
    };
    
    /**
     * 注销过程
     * 
     * <br>Created 2014-11-21 上午9:34:20
     * @author       zhangqr
     */
    private void doLogout(){
        //Toast.makeText(mActivity, "logout", Toast.LENGTH_SHORT).show();
        PaymentUtils.logout(mActivity);
    }
    
    /**
     * setmStrTitleRate
     *
     * <br>Created 2014-11-10 下午2:44:31
     * @param   mStrTitleRate    the mStrTitleRate to set
     * @author       zhangqr
     */
    public void setmStrTitleRate(String mStrTitleRate) {
        this.mStrTitleRate = mStrTitleRate;
    }

    /**
     * 设置标题
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @param mStrTitle
     *            标题
     * @author zhangqr
     */
    public void setmStrTitle(String mStrTitle) {
        this.mStrTitle = mStrTitle;
    }
    
    /**
     * 设置滚动框里面默认显示第几个
     * 
     * <br>
     * Created 2014-11-3 下午8:41:50
     * 
     * @param mIntDefaultItemIndex
     *            滚动框里面默认显示第几个
     * @author zhangqr
     */
    public void setmIntDefaultItemIndex(int mIntDefaultItemIndex) {
        this.mIntDefaultItemIndex = mIntDefaultItemIndex;
    }
    
    /**
     * 设置剩余币种数量的提示
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @param mStrRestTip
     *            剩余币种数量的提示
     * @author zhangqr
     */
    public void setmStrRestTip(String mStrRestTip) {
        this.mStrRestTip = mStrRestTip;
    }
    
    /**
     * 设置兑换率提示
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @param mStrRateTip
     *            兑换率提示
     * @author zhangqr
     */
    public void setmStrRateTip(String mStrRateTip) {
        this.mStrRateTip = mStrRateTip;
    }
    
    /**
     * 设置提示
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @param mStrTip
     *            提示
     * @author zhangqr
     */
    public void setmStrTip(String mStrTip) {
        this.mStrTip = mStrTip;
    }
    
    /**
     * 设置左按钮文本
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @param mStrBtnLeft
     *            左按钮文本
     * @author zhangqr
     */
    public void setmStrBtnLeft(String mStrBtnLeft) {
        this.mStrBtnLeft = mStrBtnLeft;
    }

    /**
     * 设置左按钮文本
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @param mStrBtnRight
     *            左按钮文本
     * @author zhangqr
     */
    public void setmStrBtnRight(String mStrBtnRight) {
        this.mStrBtnRight = mStrBtnRight;
    }

    /**
     * 设置所加载的layout资源ID
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @param mLayoutResID
     *            所加载的layout资源ID
     * @author zhangqr
     */
    public void setmLayoutResID(int mLayoutResID) {
        this.mLayoutResID = mLayoutResID;
    }

    /**
     * 设置滚动框显示数据
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @param mDataList
     *            滚动框显示数据
     * @author zhangqr
     */
    public void setmDataList(ArrayList<Object> mDataList) {
        this.mDataList = mDataList;
    }
    
    /**
     * 设置按钮点击监听
     *
     * <br>Created 2014-11-4 上午11:42:12
     * @param   mListener    the mListener to set
     * @author       zhangqr
     */
    public void setmListener(OnCommonRechargeClickListener mListener) {
        this.mListener = mListener;
    }
    
    /**
     * 设置滚动框滚动监听
     *
     * <br>Created 2014-11-4 上午11:42:12
     * @param   mWheelScrollListener    the mWheelScrollListener to set
     * @author       zhangqr
     */
    public void setmWheelScrollListener(OnWheelScrollListener mWheelScrollListener) {
        this.mWheelScrollListener = mWheelScrollListener;
    }
    
    
    /**
     * 获得标题
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @return 标题
     * @author zhangqr
     */
    public String getmStrTitle() {
        return mStrTitle;
    }

    /**
     * 获得滚动框里面默认显示第几个
     * 
     * <br>
     * Created 2014-11-3 下午8:41:50
     * 
     * @return 滚动框里面默认显示第几个
     * @author zhangqr
     */
    public int getmIntDefaultItemIndex() {
        return mIntDefaultItemIndex;
    }

    /**
     * 获得剩余币种数量的提示
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @return 剩余币种数量的提示
     * @author zhangqr
     */
    public String getmStrRestTip() {
        return mStrRestTip;
    }

    /**
     * 获得兑换率提示
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @return 兑换率提示
     * @author zhangqr
     */
    public String getmStrRateTip() {
        return mStrRateTip;
    }

    /**
     * 获得提示
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @return 提示
     * @author zhangqr
     */
    public String getmStrTip() {
        return mStrTip;
    }

    /**
     * 获得左按钮文本
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @return 左按钮文本
     * @author zhangqr
     */
    public String getmStrBtnLeft() {
        return mStrBtnLeft;
    }

    /**
     * 获得右按钮文本
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @return 右按钮文本
     * @author zhangqr
     */
    public String getmStrBtnRight() {
        return mStrBtnRight;
    }
    
    /**
     * 获得所加载的layout资源ID
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @return 所加载的layout资源ID
     * @author zhangqr
     */
    public int getmLayoutResID() {
        return mLayoutResID;
    }
    
    /**
     * 获得滚动框显示数据
     * 
     * <br>
     * Created 2014-11-3 下午7:53:15
     * 
     * @return 滚动框显示数据
     * @author zhangqr
     */
    public ArrayList<Object> getmDataList() {
        return mDataList;
    }
    
    /**
     * 获取按钮点击监听
     *
     * <br>Created 2014-11-4 上午11:42:12
     * @return       mListener
     * @author       zhangqr
     */
    public OnCommonRechargeClickListener getmListener() {
        return mListener;
    }

    /**
     * 获取滚动框滚动监听
     *
     * <br>Created 2014-11-4 上午11:42:12
     * @return       mWheelScrollListener
     * @author       zhangqr
     */
    public OnWheelScrollListener getmWheelScrollListener() {
        return mWheelScrollListener;
    }
    
    /**
     * isDismissWhenDone
     *
     * <br>Created 2014-11-4 下午8:32:28
     * @return       isDismissWhenDone
     * @author       zhangqr
     */
    public boolean isDismissWhenDone() {
        return isDismissWhenDone;
    }

    
    /**
     * setDismissWhenDone
     *
     * <br>Created 2014-11-4 下午8:32:28
     * @param   isDismissWhenDone    the isDismissWhenDone to set
     * @author       zhangqr
     */
    public void setDismissWhenDone(boolean isDismissWhenDone) {
        this.isDismissWhenDone = isDismissWhenDone;
    }
    
    /**
     * 是否显示滚动框
     *
     * <br>Created 2014-11-7 上午10:54:06
     * @return       mIsShowWheelView
     * @author       zhangqr
     */
    public boolean ismIsShowWheelView() {
        return mIsShowWheelView;
    }

    
    /**
     * 是否显示滚动框
     *
     * <br>Created 2014-11-7 上午10:54:06
     * @param   mIsShowWheelView    the mIsShowWheelView to set
     * @author       zhangqr
     */
    public void setmIsShowWheelView(boolean mIsShowWheelView) {
        this.mIsShowWheelView = mIsShowWheelView;
    }

    
    /**
     * getmStrTitleRest
     *
     * <br>Created 2014-11-10 下午2:44:31
     * @return       mStrTitleRest
     * @author       zhangqr
     */
    public String getmStrTitleRest() {
        return mStrTitleRest;
    }

    
    /**
     * setmStrTitleRest
     *
     * <br>Created 2014-11-10 下午2:44:31
     * @param   mStrTitleRest    the mStrTitleRest to set
     * @author       zhangqr
     */
    public void setmStrTitleRest(String mStrTitleRest) {
        this.mStrTitleRest = mStrTitleRest;
    }

    
    /**
     * getmStrTitleRate
     *
     * <br>Created 2014-11-10 下午2:44:31
     * @return       mStrTitleRate
     * @author       zhangqr
     */
    public String getmStrTitleRate() {
        return mStrTitleRate;
    }

    
    

}
